package com.example.log_db.controller;

import com.example.log_db.annotation.LogRecord;
import com.example.log_db.annotation.SpELLogRecord;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 日志演示控制器
 */
@RestController
@RequestMapping("/api/demo")
@RequiredArgsConstructor
public class LogDemoController {

    /**
     * 简单日志注解示例
     */
    @LogRecord(operationType = "查询", description = "查询用户信息")
    @GetMapping("/user/{id}")
    public UserResponse getUser(@PathVariable Long id) {
        // 模拟查询用户
        UserResponse user = new UserResponse();
        user.setId(id);
        user.setUsername("user" + id);
        user.setEmail("user" + id + "@example.com");
        return user;
    }

    /**
     * SpEL日志注解示例 - 简单场景
     */
    @SpELLogRecord(
        operationType = "创建",
        description = "创建新用户",
        businessType = "USER_MANAGEMENT"
    )
    @PostMapping("/user")
    public UserResponse createUser(@RequestBody UserRequest request) {
        // 模拟创建用户
        UserResponse user = new UserResponse();
        user.setId(100L);
        user.setUsername(request.getUsername());
        user.setEmail(request.getEmail());
        return user;
    }

    /**
     * SpEL日志注解示例 - 复杂场景，使用SpEL表达式
     */
    @SpELLogRecord(
        operationType = "更新",
        description = "'更新用户[' + #request.username + ']的信息'",
        businessId = "#id",
        businessType = "USER_MANAGEMENT",
        operator = "#request.operatorId",
        extra = "#result.email",
        recordParams = true,
        recordResult = true
    )
    @PutMapping("/user/{id}")
    public UserResponse updateUser(@PathVariable Long id, @RequestBody UserUpdateRequest request) {
        // 模拟更新用户
        UserResponse user = new UserResponse();
        user.setId(id);
        user.setUsername(request.getUsername());
        user.setEmail(request.getEmail());
        return user;
    }

    /**
     * SpEL日志注解示例 - 使用方法执行结果
     */
    @SpELLogRecord(
        operationType = "删除",
        description = "'删除用户[' + #result.username + ']'",
        businessId = "#id",
        businessType = "USER_MANAGEMENT",
        extra = "#result.success ? '删除成功' : '删除失败'",
        parseBeforeExecution = false
    )
    @DeleteMapping("/user/{id}")
    public DeleteResponse deleteUser(@PathVariable Long id) {
        // 模拟删除用户
        DeleteResponse response = new DeleteResponse();
        response.setSuccess(true);
        response.setUsername("user" + id);
        return response;
    }

    /**
     * SpEL日志注解示例 - 异常处理
     */
    @SpELLogRecord(
        operationType = "处理",
        description = "'处理订单[' + #orderId + ']'",
        businessId = "#orderId",
        businessType = "ORDER_PROCESSING",
        extra = "#errorMsg != null ? '处理失败: ' + #errorMsg : '处理成功'",
        parseBeforeExecution = false
    )
    @PostMapping("/order/{orderId}/process")
    public OrderResponse processOrder(@PathVariable String orderId, @RequestBody OrderProcessRequest request) {
        // 模拟处理订单，可能抛出异常
        if ("error".equals(orderId)) {
            throw new RuntimeException("订单处理失败");
        }
        
        OrderResponse response = new OrderResponse();
        response.setOrderId(orderId);
        response.setStatus("PROCESSED");
        return response;
    }

    // 请求和响应模型

    @Data
    public static class UserRequest {
        private String username;
        private String email;
    }

    @Data
    public static class UserUpdateRequest {
        private String username;
        private String email;
        private String operatorId; // 操作人ID
    }

    @Data
    public static class UserResponse {
        private Long id;
        private String username;
        private String email;
    }

    @Data
    public static class DeleteResponse {
        private boolean success;
        private String username;
    }

    @Data
    public static class OrderProcessRequest {
        private String action;
    }

    @Data
    public static class OrderResponse {
        private String orderId;
        private String status;
    }
}